import { setToken, getToken, removeToken, setStorage, getStorage, clearStorage } from '@/utils/storage.js'

const state={
    token: getToken() || null,
    userName: getStorage('username') || '',
    password: getStorage('password') || '',
    headPic: getStorage('headpic') || ''
}
const getters={

}
//同步更改state
//commit触发的是actions内的方法
const mutations={
    SET_TOKEN: (state, token) => {
        state.token = token
        setToken(token)
    },
    SET_USERNAME: (state, userName) => {
        state.userName = userName
        setStorage('username', userName)
    },
    SET_PASSWORD: (state, password) => {
        state.password = password
        setStorage('password', password)
    },
    SET_HEADPIC: (state, headPic) => {
        state.headPic = headPic
        setStorage('headpic', headPic)
    },
}
const actions={
    logout: ({ commit }) => {
        commit('SET_TOKEN', null)
        commit('SET_USERNAME', '')
        commit('SET_PASSWORD', '')
        commit('SET_HEADPIC', '')
        removeToken()
        clearStorage()
    }
}
export default{
    namespaced:true,
    state,
    getters,
    mutations,
    actions
}